package com.shouxin.attendance.network;

import com.shouxin.attendance.constants.CommonConstants;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.R.attr.tag;

@SuppressWarnings("unused")
public class HttpActionImpl implements HttpAction {

	private static final Logger logger = Logger.getLogger(HttpActionImpl.class);

	private static final MediaType JSON = MediaType.parse("application/json;");

	private final OkHttpClient client;

	public HttpActionImpl() {
		client = new OkHttpClient.Builder()
				.connectTimeout(2, TimeUnit.MINUTES)
				.writeTimeout(2, TimeUnit.MINUTES)
				.readTimeout(2, TimeUnit.MINUTES)
				.build();
	}

	private String getUrl(Map<String, String> params, String baseUrl) {
		String url = baseUrl;
		boolean first = true;
		for (Map.Entry<String, String> param : params.entrySet()) {
			if (first) {
				first = false;
				url += "?" + param.getKey() + "=" + param.getValue();
			} else {
				url += "&" + param.getKey() + "=" + param.getValue();
			}
		}
		logger.debug("request url is : " + url);
		return url;
	}

	@Override
	public void heartBeat(Map<String, String> params, Object tag) throws IOException {
		Request request = new Request.Builder()
				.url(getUrl(params, CommonConstants.HTTP_URL_HEART_BEAT))
				.tag(tag)
				.build();
		client.newCall(request).enqueue(new Callback() {
			@Override
			public void onFailure(Call call, IOException e) {
				logger.error("Heart beat send exception!", e);
			}

			@Override
			public void onResponse(Call call, Response response) throws IOException {
				logger.debug("Heart beat send success. response is : " + response);
			}
		});
	}

	@Override
	public void login(String body, Object ag, CommonCallback callback) {
		Request request = new Request.Builder()
				.post(RequestBody.create(JSON, body))
				.url(CommonConstants.HTTP_URL_LOGIN)
				.tag(tag)
				.build();
		client.newCall(request).enqueue(callback);
	}

	@Override
	public void getConfig(Map<String, String> params, Object tag, CommonCallback callback) {
		Request request = new Request.Builder()
				.url(getUrl(params, CommonConstants.HTTP_URL_CONFIG))
				.tag(tag)
				.build();
		client.newCall(request).enqueue(callback);
	}

	@Override
	public void getBabyList(Map<String, String> params, Object tag, CommonCallback callback) {
		Request request = new Request.Builder()
				.url(getUrl(params, CommonConstants.HTTP_URL_BABY_LIST))
				.tag(tag)
				.build();
		client.newCall(request).enqueue(callback);
	}

	@Override
	public void swipeCard(String body, Object tag, CommonCallback callback) {
		Request request = new Request.Builder()
				.post(RequestBody.create(JSON, body))
				.url(CommonConstants.HTTP_URL_SIGN)
				.tag(tag)
				.build();
		client.newCall(request).enqueue(callback);
	}
}
